#!/bin/bash
JAVA_HOME=/usr/lib/jdk1.8.0
DATE=$(date "+%Y-%m-%d %H:%M:%S") ####显示打印日志的时间
CONTROL="start"
SYSTEM_NAME=$1

BASH_PATH=$(cd `dirname $0`; pwd)
CURRENT_PATH=$(cd ${BASH_PATH}/../; pwd)
LOG_FILE="/data/code/Shell/log/${SYSTEM_NAME}_$(date "+%Y%m%d%H%M%S").log"
OUTPUT_FILE="/data/code/Shell/data/${SYSTEM_NAME}.text"
CONFIG_PATH="/data/code/Shell/config"
YAML_NAME="/data/code/Shell/config/${SYSTEM_NAME}.yaml"

function LOG() {
    USER=$(whoami) ####那个用户在操作
    if [ ! -e /data/code/Shell/log ];then
       mkdir /data/code/Shell/log
    fi
    if [ ! -f ${LOG_FILE} ];then
       touch ${LOG_FILE}
    fi
    echo "[${DATE}] ${USER} [$1] $2"
    echo "[${DATE}] ${USER} [$1] $2" >> ${LOG_FILE}  ######（$0脚本本身，$@将参数作为整体传输调用, >> 将输出结果以追加的方式添加重定向到本地文件中）
}

function Read_Yaml_Key(){
    flag=0
    # 逐行读取内容
    cat $1 | while read LINE
    do
        if [ $flag == 0 ];then
            # 属性开始标志 vehicle_type:
            if [ "$(echo $LINE | grep "$2:")" != "" ];then
                if [ "$(echo $LINE | grep -E ' ')" != "" ];then
            		# 截取出key值
                	echo "$LINE" | awk -F " " '{print $2}'
                	continue
            	else
            		# 如果关键词后面没有空格，则跳出继续查找
                	continue
            	fi
            fi
        fi
    done
}

function Write_system_lib() {
    RULE="so|python"
    touch ${OUTPUT_FILE}
    ls -a /usr/local/lib | grep -E ${RULE} >> ${OUTPUT_FILE}
    ls -a /usr/lib  | grep -E ${RULE} >> ${OUTPUT_FILE}
    ls -a /usr/lib64 | grep -E ${RULE} >> ${OUTPUT_FILE}
    return 0
}

function main() {
    LOG INFO "----------start $1 service --------------"
    # ssh service
    ISRUN=($(Read_Yaml_Key $YAML_NAME "ssh"))
    if [ "y" == $ISRUN ];then
        SSHD_NUM=$(ps aux | grep "/usr/sbin/sshd" | grep -v grep | wc -l)
        if [ ${SSHD_NUM} -ne 0 ];then
            LOG INFO  "ssh service is running"
        else
            /usr/sbin/sshd -D &
            LOG INFO  "ssh service start success"
        fi
    else
        LOG INFO  "ssh service set is n"
    fi

    # jupyter service
    ISRUN=($(Read_Yaml_Key $YAML_NAME "jupyter"))
    if [ "y" == $ISRUN ];then
        pid=$(ps aux | grep "jupyter" | grep -v grep | wc -l)
        if [ $pid -ne 0 ];then
            LOG INFO  "jupyter service is running"
        else
            nohup jupyter notebook --allow-root --port=2260 > /dev/null 2>&1 &
            LOG INFO  "jupyter start"
        fi
    else
        LOG INFO  "jupyter service set is n"
    fi

    # bt service
    ISRUN=($(Read_Yaml_Key $YAML_NAME "bt"))
    if [ "y" == $ISRUN ];then
        current_time=$(date "+%Y%m%d")
        if [ -e $CONFIG_PATH/.lock ];then
            file_time=$(cat $CONFIG_PATH/.lock)
            if [ $current_time == $file_time ];then
                LOG INFO "bt is running"
            else
                bt 1
                LOG INFO "bt restart"
            fi
        else
            echo "$(date "+%Y%m%d")" > $CONFIG_PATH/.lock
            bt 1
            LOG INFO "bt restart"
        fi
    else
        LOG INFO  "bt service set is n"
    fi

    # jenkins service
    ISRUN=($(Read_Yaml_Key $YAML_NAME "jenkins"))
    if [ "y" == $ISRUN ];then
        pid=`ps -ef | grep jenkins.war | grep -v 'grep'| awk '{print $2}'| wc -l`
        if [ "$CONTROL" = "start" ];then
            if [ $pid -gt 0 ];then
                LOG "INFO" 'jenkins is running...'
            else ### jenkins start
                nohup java -jar /usr/lib/jenkins/jenkins.war --httpPort=8080  2>&1 &
                LOG "INFO" 'jenkins is restart'
            fi
        elif [ "$CONTROL" = "stop" ]; then
            if [ $pid -gt 0 ];then
                exec ps -ef | grep jenkins | grep -v grep | awk '{print $2}'| xargs kill -9
                LOG "INFO" 'jenkins is stop..'
            fi
        else
            LOG "ERROR" "Please input like this:"./jenkins.sh start" or "./jenkins stop""
        fi
    else
        LOG INFO "jenkins service set is n"
    fi

    Write_system_lib
    LOG INFO "------------end----------------"
}

main